{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b9d4246e",
   "metadata": {},
   "source": [
    "# Week07_note"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1320bc8f",
   "metadata": {},
   "source": [
    "## 一、元组（Tuple）\n",
    "### 元组只有两个方法：count 和 index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8d2cfcfb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(12345, 54321, 'hello!')\n",
      "<class 'tuple'>\n",
      "0\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "t = 12345,54321,'hello!'\n",
    "print(t)\n",
    "print(type(t))\n",
    "print(t.index(12345))\n",
    "print(t.count(12345))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2bc8e3a2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))\n"
     ]
    }
   ],
   "source": [
    "# 例子\n",
    "u = t, (1, 2, 3, 4, 5)\n",
    "print(u)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c1dfb14",
   "metadata": {},
   "source": [
    "## 二、 课本练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "89cf7e5b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
      "第一次改变后的 ['o', 'n', 't', ' ', 'p', 'a']\n",
      "第二次改变后的 ['o', 'n', 't', ' ', 'a', 'p']\n",
      "第三次改变后的 ['o', 'n', ' ', 't', 'a', 'p']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "最后结果： on tap\n"
     ]
    }
   ],
   "source": [
    "##  方法一\n",
    "\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)  # list()可以将 str to(转换成) list\n",
    "print(phrase)\n",
    "print(plist)\n",
    "## 删除后面三位元素\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "print(plist)\n",
    "\n",
    "## 删除 D ‘\n",
    "plist.pop(0)\n",
    "plist.pop(2)\n",
    "print(\"第一次改变后的\",plist)\n",
    "# # 或 plist.remove(\"D\")\n",
    "# plist.remove(\"'\")\n",
    "\n",
    "# plist.extend()\n",
    "# 数学案例：计算（（a+b）*（c+d））\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "#['o', 'n', 't', ' '] [a , p ]\n",
    "print(\"第二次改变后的\",plist)\n",
    "\n",
    "# insert\n",
    "# plist.insert(2,plist.pop(3))\n",
    "# print(plist)\n",
    "# print(plist.pop(2)) # 1.代表删除的内容的值 2.他删除了plist这个值\n",
    "plist.insert(3,plist.pop(2))\n",
    "print(\"第三次改变后的\",plist)\n",
    "\n",
    "# list to str (join之前的’‘代表以什么将字符串合并)\n",
    "new_phrase = ''.join(plist)\n",
    "print(plist)\n",
    "print(\"最后结果：\",new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "632d54c8",
   "metadata": {},
   "source": [
    "### plist 改变了原先的状态吗？列表的方法会改变列表本身，如果不想打破或者想保留原先数据的内容，请用copy()复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5b379bb9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "转化为列表后的 ['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['o', 'n']\n",
      "on\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "##   方法二——切片\n",
    "\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)  # list()可以将 str to(转换成) list\n",
    "print(phrase)\n",
    "print(\"转化为列表后的\",plist)\n",
    "print(plist[1:3])\n",
    "on_str = ''.join(plist[1:3])\n",
    "print(on_str)\n",
    "new_phrase = on_str + ''.join(plist[5]+plist[4]+plist[7]+plist[6])\n",
    "# print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76a36487",
   "metadata": {},
   "source": [
    "## 三、 购物车项目（初尝试）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "25deee46",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的余额：100\n",
      "------welcome 登录------\n",
      "------goods list------\n",
      "('1001', 'python课本', 88)\n",
      "('1002', '蜜雪冰城—冰茶', 10)\n",
      "('1003', 'iphone13 pro', 9999)\n",
      "('1004', '华为电—跑车', 400000)\n",
      "('1005', '小米手环', 199)\n",
      "\n",
      "请输入商品的编号：1001\n",
      "('1001', 'python课本', 88)\n",
      "您选择的商品是： python课本\n",
      "购买商品 ('1001', 'python课本', 88) 成功您的余额为 12 元！\n"
     ]
    }
   ],
   "source": [
    "goods = [\n",
    "    ('1001',\"python课本\",88),\n",
    "    ('1002',\"蜜雪冰城—冰茶\",10),\n",
    "    ('1003',\"iphone13 pro\",9999),\n",
    "    ('1004',\"华为电—跑车\",400000),\n",
    "    ('1005',\"小米手环\",199)\n",
    "]\n",
    "\n",
    "shopping_list = []\n",
    "user_salary = input(\"请输入你的余额：\")\n",
    "info = '''------welcome 登录------\n",
    "------goods list------\n",
    "%s\n",
    "%s\n",
    "%s\n",
    "%s\n",
    "%s\n",
    "'''%(goods[0],goods[1],goods[2],goods[3],goods[4])\n",
    "print(info)\n",
    "\n",
    "if user_salary.isdigit():\n",
    "    user_salary = int(user_salary)\n",
    "    user_input_ID = input(\"请输入商品的编号：\")\n",
    "    for product in goods:\n",
    "        print(product)\n",
    "        if user_input_ID == product[0]:\n",
    "            print(\"您选择的商品是：\", product[1])\n",
    "            if user_salary >= product[2]:\n",
    "                shopping_list.append(product)\n",
    "                user_salary -= product[2]\n",
    "                print(\"购买商品\", product, \"成功您的余额为\", user_salary, \"元！\")\n",
    "                break\n",
    "            else:\n",
    "                print(\"您的余额为\", user_salary, \"余额不足以购买此商品，购买失败！\")\n",
    "                break\n",
    "        else:\n",
    "            print(\"您输入的商品信息有问题，请输入正确的商品编号。\")\n",
    "\n",
    "\n",
    "else:\n",
    "    print(\"您的输入有误，请重新输入!\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ee5c3cf",
   "metadata": {},
   "source": [
    "## 四、 课本练习题（P61）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1b007e5b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:Milliways\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = []\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
